/*****************************************************************************
* Keppe Messenger (KMP) Keppe Project-Team User Messaging System
* Copyright (C) 2013-2014 Keppe Project-Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

#include "frmAboutKMPQ.h"
#include "ui_frmAboutKMPQ.h"
#include <QFile>
QObject *formAK;

frmAboutKMPQ::frmAboutKMPQ(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::frmAboutKMPQ)
{
    setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
    ui->setupUi(this);
#ifdef MACX
    this->setMinimumSize(350,400);
    this->setMaximumSize(350,400);
    this->resize(350,400);
#else
#ifdef UNIX
    this->setMinimumSize(350,400);
    this->setMaximumSize(350,400);
    this->resize(350,400);
#endif
#endif
    // Not edit this, this is old
    ui->labelAbout->setText("Keppe Messenger by Keppe Project-Team Version " + qApp->applicationVersion()  + "<br/><br/>Keppe Messenger is a secure messenger that's protect your privacy for you, it's allow you to chat complete private without overheard from others<br><br>Copyright &copy; KeppePT 2013-2014");
    // Read Credits
    if (QFile::exists(":/gui/credits.html"))
    {
        QFile *rf = new QFile();
        rf->setFileName(":/gui/credits.html");
        rf->open(QFile::ReadOnly);
        QByteArray rb = rf->readAll();
        rf->close();
        rf->deleteLater();
        credits = QString::fromUtf8(rb);
    }
}

frmAboutKMPQ::~frmAboutKMPQ()
{
    delete ui;
}

void frmAboutKMPQ::on_cmdOK_clicked()
{
    this->close();
}

void frmAboutKMPQ::setForm(frmLogin* login)
{
    formAK = login;
}

// config.h -> AboutProduct for Info Text

void frmAboutKMPQ::updateText()
{
    int dtd = 0;
#ifdef __DATE__
    dtd++;
    QString buildDate = __DATE__;
#ifdef __TIME__
    dtd++;
    QString buildTime = __TIME__;
#endif
#endif
    frmLogin *login = (frmLogin*)formAK;
    QString fpc = "Qt " + QString(QT_VERSION_STR) + ", " + QString(ProductCompiler);
    QString rqt = qVersion();
    if (dtd == 0)
    {
        ui->labelAbout->setText(QString("<span style=\"font-weight: bold;\">Version</span><br/>" + QString(ProductName) + " Client " + qApp->applicationVersion()  + "<br/>" + "Keppe Messenger Fw " + login->getMessengerFrameworkVersion() + "<br/>" + "Keppe Gui Fw " + login->getGuiFrameworkVersion() + "<br/><br/><span style=\"font-weight: bold;\">Build</span><br/>Built with %1<br/>Running with Qt %2 on %3<br/><br/>" + credits + "<span style=\"font-weight: bold;\">Info</span><br/>" + QString(AboutProduct)).arg(fpc,rqt,login->hostName));
    }
    else if (dtd == 1)
    {
        ui->labelAbout->setText(QString("<span style=\"font-weight: bold;\">Version</span><br/>" + QString(ProductName) + " Client " + qApp->applicationVersion()  + "<br/>" + "Keppe Messenger Fw " + login->getMessengerFrameworkVersion() + "<br/>" + "Keppe Gui Fw " + login->getGuiFrameworkVersion() + "<br/><br/><span style=\"font-weight: bold;\">Build</span><br/>Built on %1 with %2<br/>Running with Qt %3 on %4<br/><br/>" + credits + "<span style=\"font-weight: bold;\">Info</span><br/>" + QString(AboutProduct)).arg(buildDate,fpc,rqt,login->hostName));
    }
    else if (dtd == 2)
    {
        ui->labelAbout->setText(QString("<span style=\"font-weight: bold;\">Version</span><br/>" + QString(ProductName) + " Client " + qApp->applicationVersion()  + "<br/>" + "Keppe Messenger Fw " + login->getMessengerFrameworkVersion() + "<br/>" + "Keppe Gui Fw " + login->getGuiFrameworkVersion() + "<br/><br/><span style=\"font-weight: bold;\">Build</span><br/>Built on %1 at %2 with %4<br/>Running with Qt %5 on %6<br/><br/>" + credits + "<span style=\"font-weight: bold;\">Info</span><br/>" + QString(AboutProduct)).arg(buildDate,buildTime,fpc,rqt,login->hostName));
    }

}

void frmAboutKMPQ::on_labelAbout_linkActivated(const QString &link)
{
    if (link.left(17) == "kmp://addcontact/")
    {
        QString username = link;
        username = username.remove(0,17);
        frmLogin *login = (frmLogin*)formAK;
        login->addUserContactWithItem(username,this);
    }
}

void frmAboutKMPQ::on_bbOK_clicked(QAbstractButton *button)
{
    if (ui->bbOK->buttonRole(button) == QDialogButtonBox::AcceptRole)
    {
        on_cmdOK_clicked();
    }
}
